unit ImportDBHandle;

interface

uses
  Classes, ADODB, SysUtils;


type


  TDBFieldType =
    (
    Field_Number,
    Field_String,
    Field_DateTime
    );



  TDBFieldItem = class(TCollectionItem)
  private
    FCanNull: Boolean;
    FFieldType: TDBFieldType;
    FNo: Integer;
    FField: string;
    FTitle: string;
    FValue: string;
    FMaxLen: Integer;
    FCheckRepeat: Boolean;
    FIsShow: Boolean;
    procedure SetCanNull(const Value: Boolean);
    procedure SetField(const Value: string);
    procedure SetFieldType(const Value: TDBFieldType);
    procedure SetNo(const Value: Integer);
    procedure SetTitle(const Value: string);
    procedure SetValue(const Value: string);
    procedure SetMaxLen(const Value: Integer);
    procedure SetCheckRepeat(const Value: Boolean);
    procedure SetIsShow(const Value: Boolean);
  published
  public
    property No: Integer read FNo write SetNo;
    property Field: string read FField write SetField;
    property Title: string read FTitle write SetTitle;
    property FieldType: TDBFieldType read FFieldType write SetFieldType;
    property CanNull: Boolean read FCanNull write SetCanNull;
    property Value: string read FValue write SetValue;
    property MaxLen: Integer read FMaxLen write SetMaxLen;
    property CheckRepeat: Boolean read FCheckRepeat write SetCheckRepeat;
    property IsShow: Boolean read FIsShow write SetIsShow;
  end;


  TDBFieldCollection = class(TCollection)
  private
    function GetItem(Index: Integer): TDBFieldItem;
    procedure SetItem(Index: Integer; const Value: TDBFieldItem);
  published
  public
    function Add: TDBFieldItem;
    function FindItemID(ID: Integer): TDBFieldItem;
    function Insert(Index: Integer): TDBFieldItem;
    property Items[Index: Integer]: TDBFieldItem read GetItem write SetItem;
  end;


  EImportDBhandle = Exception;

  TImportDBHandle = class
  private
    FADOConn: TADOConnection;
    FQry: TADOQuery;
    FDBFields: TDBFieldCollection;
    FOnImportData: TNotifyEvent;
    FTableName: string;
    FImportResultInfo: string;
    FIsImportSuccess: Boolean;
    procedure SetDBFields(const Value: TDBFieldCollection);
    procedure SetOnImportData(const Value: TNotifyEvent);
    procedure SetTableName(const Value: string);
    procedure SetImportResultInfo(const Value: string);
    procedure SetIsImportSuccess(const Value: Boolean);
    procedure SetQry(const Value: TADOQuery);

  public
    constructor Create(aADOConn: TADOConnection);
    destructor Destroy; override;

    procedure ImportData(); virtual; abstract;

    procedure RaiseError(aError: string);

    property OnImportData: TNotifyEvent read FOnImportData write SetOnImportData;
    property DBFields: TDBFieldCollection read FDBFields write SetDBFields;
    property TableName: string read FTableName write SetTableName;

    property IsImportSuccess: Boolean read FIsImportSuccess write SetIsImportSuccess;
    property ImportResultInfo: string read FImportResultInfo write SetImportResultInfo;
    property Qry: TADOQuery read FQry write SetQry;

  end;


  TFileImportDBHandle = class(TImportDBHandle)
  private
    FFileName: string;
    procedure SetFileName(const Value: string);
  protected
    procedure BeforeImport; virtual; abstract;
    procedure AfterImport; virtual; abstract;
    function GetDataCount: Integer; virtual; abstract;
    procedure FetchOneData(index: integer; OneData: TDBFieldCollection); virtual; abstract;
    procedure SaveOneData(OneData: TDBFieldCollection); virtual; abstract;
  published
  public
    procedure ImportData; override;
    property FileName: string read FFileName write SetFileName;
  end;



implementation

{ TDBFieldItem }

procedure TDBFieldItem.SetCanNull(const Value: Boolean);
begin
  FCanNull := Value;
end;

procedure TDBFieldItem.SetCheckRepeat(const Value: Boolean);
begin
  FCheckRepeat := Value;
end;

procedure TDBFieldItem.SetField(const Value: string);
begin
  FField := Value;
end;

procedure TDBFieldItem.SetFieldType(const Value: TDBFieldType);
begin
  FFieldType := Value;
end;

procedure TDBFieldItem.SetIsShow(const Value: Boolean);
begin
  FIsShow := Value;
end;

procedure TDBFieldItem.SetMaxLen(const Value: Integer);
begin
  FMaxLen := Value;
end;

procedure TDBFieldItem.SetNo(const Value: Integer);
begin
  FNo := Value;
end;

procedure TDBFieldItem.SetTitle(const Value: string);
begin
  FTitle := Value;
end;

procedure TDBFieldItem.SetValue(const Value: string);
begin
  FValue := Value;
end;

{ TDBFieldCollection }

function TDBFieldCollection.Add: TDBFieldItem;
begin
  Result := TDBFieldItem(inherited Add);
end;

function TDBFieldCollection.FindItemID(ID: Integer): TDBFieldItem;
begin
  Result := TDBFieldItem(inherited FindItemID(ID));
end;

function TDBFieldCollection.GetItem(Index: Integer): TDBFieldItem;
begin
  Result := TDBFieldItem(inherited GetItem(Index));
end;

function TDBFieldCollection.Insert(Index: Integer): TDBFieldItem;
begin
  Result := TDBFieldItem(inherited Insert(Index));
end;


procedure TDBFieldCollection.SetItem(Index: Integer; const Value: TDBFieldItem);
begin
  inherited SetItem(Index, Value);
end;


{ TImportDBHandle }

constructor TImportDBHandle.Create(aADOConn: TADOConnection);
begin
  FADOConn := aADOConn;
  FQry := TADOQuery.Create(nil);
  FQry.Connection := FADOConn;
  FDBFields := TDBFieldCollection.Create(TDBFieldItem);
end;

destructor TImportDBHandle.Destroy;
begin
  FQry.Free;
  FDBFields.Free;
  inherited;
end;


procedure TImportDBHandle.RaiseError(aError: string);
begin
  raise EImportDBhandle.Create(aError);
end;

procedure TImportDBHandle.SetDBFields(const Value: TDBFieldCollection);
begin
  FDBFields := Value;
end;

procedure TImportDBHandle.SetImportResultInfo(const Value: string);
begin
  FImportResultInfo := Value;
end;

procedure TImportDBHandle.SetIsImportSuccess(const Value: Boolean);
begin
  FIsImportSuccess := Value;
end;

procedure TImportDBHandle.SetOnImportData(const Value: TNotifyEvent);
begin
  FOnImportData := Value;
end;

procedure TImportDBHandle.SetQry(const Value: TADOQuery);
begin
  FQry := Value;
end;

procedure TImportDBHandle.SetTableName(const Value: string);
begin
  FTableName := Value;
end;


{ TFileImportDBHandle }

procedure TFileImportDBHandle.ImportData;
var
  aCount: Integer;
  i: Integer;
begin
  inherited;
  BeforeImport;
  try
    aCount := GetDataCount;

    for i := 1 to aCount - 1 do
    begin
      FetchOneData(i, DBFields);
      SaveOneData(DBFields);
      if Assigned(OnImportData) then
        OnImportData(Self);
    end;
  finally
    AfterImport;
  end;

end;


procedure TFileImportDBHandle.SetFileName(const Value: string);
begin
  FFileName := Value;
end;

end.
